<template>
  <view class="content">
    <view class="head">
      <image class="textBg" src="@/static/Home_head.png"></image>
      <view class="title">医疗废物填报</view>
      <view class="mark">医废处理，从同兴做起</view>
    </view>

    <view class="mianBody">
      <fromCard ref="child" :fromData='fromData' :rules='rules' :userInfo='userInfo' @showSelect='showSelect'
                @field-change="fieldChange">
        <view class="fouter" @click="submit">确认下单</view>
      </fromCard>
    </view>
    <u-picker :columns="userList" :show="show" confirmColor='#3DB020' keyName='contactsName' @cancel='show = false'
              @confirm='confirm'></u-picker>
  </view>
</template>

<script>
import {detail, medicalWasteAdd} from '../../utils/api.js'

export default {
  data() {
    return {
      userInfo: {
        infectionBarrelsCount: '',
        infectionGrossWeight: '',
        infectionNetWeight: '',
        sharpsBarrelsCount: '',
        sharpsGrossWeight: '',
        sharpsNetWeight: '',
        totalWasteNetWeight: '',
        transferPerson: '',
        transferPersonMobile: ''
      },
      rules: {
        'totalWasteNetWeight': [{
          required: true,
          message: '请输入合计（净重）',
          trigger: ['blur', 'change']
        }],
        'transferPerson': [{
          required: true,
          message: '请输入交接人姓名',
          trigger: ['blur', 'change']
        }]
      },
      fromData: [{
        title: '感染性医疗废物（kg)(841-001-01)',
        fromItem: [{
          label: '桶数',
          key: 'infectionBarrelsCount',
          dw: '桶',
          type: 'input',
          labelWidth: 120,
          typeValue: 'number',
          placeholder: '请输入医疗废物桶数'
        }, {
          label: '毛重',
          key: 'infectionGrossWeight',
          dw: 'kg',
          type: 'input',
          typeValue: 'digit',
          labelWidth: 120,
          placeholder: '请输入医疗废物毛重'
        }, {
          label: '净重',
          key: 'infectionNetWeight',
          dw: 'kg',
          type: 'input',
          typeValue: 'digit',
          labelWidth: 120,
          placeholder: '请输入医疗废物净重'
        },],
      },
        {
          title: '损伤性医疗废物（kg)(841-002-01)',
          fromItem: [{
            label: '桶数',
            key: 'sharpsBarrelsCount',
            dw: '桶',
            typeValue: 'number',
            type: 'input',
            labelWidth: 120,
            placeholder: '请输入医疗废物桶数'
          }, {
            label: '毛重',
            key: 'sharpsGrossWeight',
            dw: 'kg',
            typeValue: 'digit',
            type: 'input',
            labelWidth: 120,
            placeholder: '请输入医疗废物毛重'
          }, {
            label: '净重',
            key: 'sharpsNetWeight',
            dw: 'kg',
            type: 'input',
            labelWidth: 120,
            typeValue: 'digit',
            placeholder: '请输入医疗废物净重'
          },],
        },
        {
          title: '合计',
          fromItem: [{
            label: '合计（净重）',
            key: 'totalWasteNetWeight',
            dw: 'kg',
            type: 'input',
            labelWidth: 120,
            placeholder: '请输入医疗废物合计重量',
            typeValue: 'digit',
            require: true
          },
            {
              label: '毛重',
              dw: 'kg',
              type: 'mark',
              labelWidth: 120,
            },
            {
              label: '交接人签字',
              key: 'transferPerson',
              dw: '',
              type: 'select',
              require: true,
              labelWidth: 120,
            },
          ],
        },
      ],
      infectinjure: [{
        label: '桶数',
        key: 'injurets',
        dw: '桶'
      },
        {
          label: '毛重',
          key: 'injuremz',
          dw: 'kg'
        },
        {
          label: '净重',
          key: 'injurejz',
          dw: 'kg'
        }
      ],

      infecttotal: [{
        label: '净重',
        dw: 'jz'
      }, {
        label: '交接人',
        key: 'jz',
        dw: 'jz'
      }],
      show: false,
      userList: []
    }
  },
  onShow() {
		this.userList = []
    this.getOrzDetail()
    // console.log('交接人：', JSON.parse(uni.getStorageSync('detail')).person)
    if (JSON.parse(uni.getStorageSync('detail'))) {
      let person = JSON.parse(uni.getStorageSync('detail'))
      this.setTransferPerson(person.persons[0].contactsName, person.persons[0].contactsMobile)
      let arr = JSON.parse(uni.getStorageSync('detail')).persons
      let arr2
      if (arr.length > 0) {
        arr2 = arr.map(el => {
          return {
            contactsName: el.contactsName,
            contactsMobile: el.contactsMobile
          }
        })
        this.userList.push(arr2)
      }
    }
  },
  methods: {
    getOrzDetail() {
      console.log('1111')
      detail().then(val => {
        let detail = JSON.stringify(val.data)
        uni.setStorageSync('detail', detail)
      }).catch(err => {
        uni.$u.toast('医疗机构信息获取失败！')
      })
    },
    showSelect() {
      this.show = true
    },
    setTransferPerson(name, mobile) {
      this.userInfo.transferPerson = name
      this.userInfo.transferPersonMobile = mobile
    },
    fieldChange(field, formData) {
      if (['sharpsNetWeight', 'infectionNetWeight'].includes(field)) {
        formData.totalWasteNetWeight = "" + (Number(formData.sharpsNetWeight || 0) + Number(formData.infectionNetWeight || 0))
      }
    },
    confirm(e) {
      // console.log('e:', e)
      this.userInfo.transferPerson = e.value[0].contactsName
      this.userInfo.transferPersonMobile = e.value[0].contactsMobile
      this.show = false
    },
    submit() {
      let child = this.$refs.child
      let detail = JSON.parse(uni.getStorageSync('detail'))
      child.$refs.form.validate().then(res => {
        medicalWasteAdd({
          ...child.from,
          mdcInstitutionId: detail.id
        }).then(res => {
          uni.$u.toast(res.message);
          if (res.success) {
            uni.setStorageSync('tab', 0)
            setTimeout(() => {
              uni.switchTab({
                url: '/pages/Receiving/Receiving'
              })
            }, 1500)
            this.userInfo = {
              infectionBarrelsCount: '',
              infectionGrossWeight: '',
              infectionNetWeight: '',
              sharpsBarrelsCount: '',
              sharpsGrossWeight: '',
              sharpsNetWeight: '',
              totalWasteNetWeight: '',
              transferPerson: '',
              transferPersonMobile: ''
            }
          }
        }).catch(err => {
          uni.$u.toast('添加失败!');
        })
      }).catch(errors => {
        uni.$u.toast('校验失败')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.textBg {
  position: absolute;
  z-index: -1;
  width: 750rpx;
  height: 420rpx;
}

.head {
  width: 750rpx;
  height: 420rpx;
  position: fixed;
  top: 0;
  z-index: 1;

  .title {
    font-size: 52rpx;
    font-family: PingFang SC, PingFang SC-Semibold;
    font-weight: bold;
    text-align: left;
    color: #256a14;
    padding-top: 224rpx;
    padding-left: 30rpx;
  }

  .mark {
    font-size: 26rpx;
    font-family: PingFang SC, PingFang SC-Regular;
    font-weight: Regular;
    text-align: left;
    color: #3db020;
    line-height: 60rpx;
    padding-left: 30rpx;
  }
}

.mianBody {
  width: 100%;
  padding: 20rpx 20rpx;
  position: relative;
  top: 420rpx;
  box-sizing: border-box;
  height: calc(100vh - 420rpx);
  overflow: auto;
}

/deep/ .u-form-item {
  padding: 20rpx 12rpx;
}

/deep/ .u-button {
  border: none !important;
}

.fouter {
  width: 100%;
  height: 100rpx;
  background: #3db020;
  border-radius: 10rpx;
  margin: auto;
  font-size: 34rpx;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: Regular;
  text-align: center;
  color: #ffffff;
  line-height: 90rpx;
  margin-top: 20rpx;
}
</style>
